﻿using System;
using System.Globalization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace Notes
{
    public class ImageConverter : IValueConverter
    {
        private IsolatedStorageFile isoStore;
        private IsolatedStorageFileStream stream;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            BitmapImage bi = new BitmapImage();

            using (isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoStore.FileExists((string)value))
                {
                    using (stream = isoStore.OpenFile((string)value, FileMode.Open))
                    {
                        bi.SetSource(stream);
                        stream.Close();
                        stream.Dispose();
                    }
                }
                isoStore.Dispose();
            }

            return bi;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
